<?php
namespace app\admin\controller;
use app\BaseController;
use think\facade\Cache;
use think\facade\Request;
use think\facade\Db;
//use think\facade\Route;
use think\facade\View;
class Good
{
    public function goodList()
    {
            //View::assign(Request::post());
            $keywords= Request::post('keywords');
             $begin= Request::post('begin');
             $end= Request::post('end');
             $xq= Request::post('xq');
             $dq= Request::post('dq');
             $cid= Request::post('cid');
            $post = Request::post();
            $where = [];
            if (isset($keywords) && $keywords <> '')                    $where[] = ['g.name','like',"%{$keywords}%"];
            if (isset($post['is_show']) && $post['is_show'] <> "")      $where[] = ['is_show','=',$post['is_show']];
            if (isset($post['is_down']) && count($post['is_down'])==1)  $where[] = ['is_down','in',$post['is_down']];
            if (isset($post['is_hot']) && $post['is_hot'] <>"")        $where[] = ['is_hot','=',$post['is_hot']];
            if (isset($post['begin']) && $post['begin'] <> '' &&  isset($post['end']) && $post['end'] <> '') $where[] = ['g.addtime','BETWEEN TIME',"{$post['begin']},{$post['end']}"];
            if (isset($post['xq']) && $post['xq'] <> '' &&  isset($post['dq']) && $post['dq'] <> '') $where[] = ['price','BETWEEN',"{$post['xq']},{$post['dq']}"];
            if (isset($post['cid']) && !empty($post['cid']))                    $where[] = ['c.id','=',$post['cid']];
            View::assign('keywords',$keywords);
            View::assign($post);

            $list = Db::table('ecm_goods')->alias('g')
                ->join('ecm_category c','g.cid = c.id')
                ->field('g.id,g.name,c.name as cname,price,photo_x,g.addtime,is_show,is_down,is_hot')
                ->where($where)
                ->order('id asc')
                ->select();

        $lista = Db::table('ecm_category')
            ->where('parentid','=','0')
            ->select();

        $listb = Db::table('ecm_category')
            ->select();

        View::assign('lista',$lista);
        View::assign('listb',$listb);
        View::assign('list',$list);
        View::assign('begin',$begin);
        View::assign('end',$end);
        View::assign('xq',$xq);
        View::assign('dq',$dq);
        View::assign('cid',$cid);


        //方案二
        //使用缓存
//        if(Cache::has('cate')){ //判断缓存变量是否存在
//            $cateAll = Cache::get('cate');  //获取缓存数据
//        }else{
//            $cateAll = Db::table('ecm_category')->select()->toArray();
//            Cache::set('cate',$cateAll,100); //设置缓存变量
//        }
//        $cate = [];
//        //循环所有分类  $v为每条分类
//        foreach ($cateAll as $v){
//            //判断 分类中parentid字段等于0 进入判断结果 都是一级分类
//            if($v['parentid'] == 0){
//                //将$v一级分类赋值到空数组中  索引为自己的ID
//                $cate[$v['id']] = $v;
//                //循环所有分类
//                foreach ($cateAll as $vv){
//                    //如果分类的parentid等于上一级分类(一级分类)ID 进入判断里的是二级分类
//                    if($vv['parentid'] == $v['id']){
//                        //将二级分类赋值到数组  根据索引($v.id) 将一级和二级绑定到一起
//                        $cate[$v['id']]['son'][$vv['id']] = $vv;
//                    }
//                }
//            }
//        }
//

        //$cate = $this->getCate($cateAll,0);
        //View::assign('cate',$cate);
       return View::fetch();
    }

//    public function getCate($cateAll,$parentid)
//    {
//        $cate=[];
//        foreach ($cateAll as $v){
//            if ($v['parentid'] == $parentid){
//                $cate[$v['id']]=$v;
//                $cate[$v['id']['son']]=$this->getCate($cateAll,$v['id']);
//
//            }
//        }
//        return $cate;
//    }

    public function goodAdd()
    {
        return view();
    }
    public function goodEdit()
    {
        return view();
    }
}